import 'dart:async';
import 'dart:io';

import 'package:acaleph/l10n/AppLocalizations.dart';
import 'package:acaleph/model/base_model.dart';
import 'package:acaleph/widget/common_snakeBar.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:acaleph/model/test_list2_model.dart';
import 'package:acaleph/mvp/presenter/test_list_presenter.dart';
import 'package:acaleph/mvp/presenter/test_list_presenter_impl.dart';
import 'package:acaleph/validate/common.dart';
import 'package:acaleph/widget/common_divider.dart';
import 'package:acaleph/widget/common_picker_simple.dart';

class FormDemo extends StatefulWidget {
  const FormDemo({Key key}) : super(key: key);

  static const String routeName = '/material/text-form-field';

  @override
  //FormDemoState createState() => new FormDemoState();
  State<StatefulWidget> createState() {
    FormDemoState view = new FormDemoState();
    TestListPresenter presenter = new TestListPresenterImpl(view);
    presenter.init();
    return view;
  }
}

class PersonData {
  String name = '';
  String page = '';
  String url = '';
  String password = '';
}

class PasswordField extends StatefulWidget {
  const PasswordField({
    this.fieldKey,
    this.hintText,
    this.labelText,
    this.helperText,
    this.onSaved,
    this.validator,
    this.onFieldSubmitted,
  });

  final Key fieldKey;
  final String hintText;
  final String labelText;
  final String helperText;
  final FormFieldSetter<String> onSaved;
  final FormFieldValidator<String> validator;
  final ValueChanged<String> onFieldSubmitted;

  @override
  _PasswordFieldState createState() => new _PasswordFieldState();
}

class _PasswordFieldState extends State<PasswordField> {
  bool _obscureText = true;

  @override
  Widget build(BuildContext context) {
    return new TextFormField(
      key: widget.fieldKey,
      obscureText: _obscureText,
      maxLength: 8,
      onSaved: widget.onSaved,
      validator: widget.validator,
      onFieldSubmitted: widget.onFieldSubmitted,
      decoration: new InputDecoration(
        border: const UnderlineInputBorder(),
        filled: true,
        hintText: widget.hintText,
        labelText: widget.labelText,
        helperText: widget.helperText,
        suffixIcon: new GestureDetector(
          onTap: () {
            setState(() {
              _obscureText = !_obscureText;
            });
          },
          child:
              new Icon(_obscureText ? Icons.visibility : Icons.visibility_off),
        ),
      ),
    );
  }
}

class FormDemoState extends State<FormDemo> implements TestListView {
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  TestListPresenter _testListPresenter;

  UserInfo _userInfo;
  UserInfo person;

  final List<String> _allActivities = <String>[
    'hiking',
    'swimming',
    'boating',
    'fishing',
    'a',
    'b',
    'c',
    'd',
    'e',
    'f',
    'g',
    'h',
    'a1',
    'b1',
    'c1',
    'd1',
    'e1',
    'f1',
    'g1',
    'h1',
  ];
  String _activity = 'fishing';
  CommonValidate commonValidate = new CommonValidate();

  void showInSnackBar(String value) {
    _scaffoldKey.currentState
        .showSnackBar(new SnackBar(content: new Text(value)));
  }

  @override
  void initState() {
    super.initState();
    _refreshData();
    setState(() {});
  }

  Future<Null> _refreshData() {
    final Completer<Null> completer = new Completer<Null>();

    _testListPresenter.loadList('11');

    completer.complete(null);

    return completer.future;
  }

  bool _autovalidate = false;
  bool _formWasEdited = false;

  final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
  final GlobalKey<FormFieldState<String>> _passwordFieldKey =
      new GlobalKey<FormFieldState<String>>();
  final _UsNumberTextInputFormatter _phoneNumberFormatter =
      new _UsNumberTextInputFormatter();

  void _handleSubmitted() {
    final FormState form = _formKey.currentState;
    if (!form.validate()) {
      _autovalidate = true; // Start validating on every change.
      showInSnackBar('Please fix the errors in red before submitting.');
    } else {
      form.save(); //执行textfield中的onsaved方法
      //执行保存请求
      showInSnackBar('${person.name}\'s phone number is ${person.page}');
    }
  }

  String _validateName(String value) {
    _formWasEdited = true;
    return commonValidate.validateName(value);
  }

  String _validatePhoneNumber(String value) {
    _formWasEdited = true;
    return commonValidate.validatePhoneNumber(value);
  }

  String _validatePassword(String value) {
    _formWasEdited = true;
    final FormFieldState<String> passwordField = _passwordFieldKey.currentState;
    if (passwordField.value == null || passwordField.value.isEmpty)
      return 'Please enter a password.';
    if (passwordField.value != value) return 'The passwords don\'t match';
    return null;
  }

  Future<bool> _warnUserAboutInvalidData() async {
    final FormState form = _formKey.currentState;
    if (form == null || !_formWasEdited || form.validate()) return true;

    return await showDialog<bool>(
          context: context,
          builder: (BuildContext context) {
            return new AlertDialog(
              title: const Text('This form has errors'),
              content: const Text('Really leave this form?'),
              actions: <Widget>[
                new FlatButton(
                  child: const Text('YES'),
                  onPressed: () {
                    Navigator.of(context).pop(true);
                  },
                ),
                new FlatButton(
                  child: const Text('NO'),
                  onPressed: () {
                    Navigator.of(context).pop(false);
                  },
                ),
              ],
            );
          },
        ) ??
        false;
  }

  @override
  Widget build(BuildContext context) {
    if (null != _userInfo && null != _userInfo.name) {
      return new Scaffold(
        key: _scaffoldKey,
        resizeToAvoidBottomPadding: false,
        appBar: new AppBar(
          title: const Text('Text fields'),
        ),
        body: new SafeArea(
          top: false,
          bottom: false,
          child: new Form(
            key: _formKey,
            autovalidate: _autovalidate,
            onWillPop: _warnUserAboutInvalidData,
            child: new SingleChildScrollView(
              padding: const EdgeInsets.symmetric(horizontal: 16.0),
              child: new Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  const SizedBox(height: 14.0),
                  new GestureDetector(
                    child:new TextFormField(
                      textAlign: TextAlign.end,
                      decoration: new InputDecoration(

                        helperText: 'helperText',
//                      icon: const Icon(Icons.person),
                        hintText: 'What do people call you?',
                        labelText: AppLocalizations.of(context).textField,
                      ),
                    ),

                  ),

                  const SizedBox(height: 14.0),
                  new TextFormField(
                    textAlign: TextAlign.end,
//                    initialValue:person.name,
                    decoration: new InputDecoration(
                      helperText: 'helperText',
//                      icon: const Icon(Icons.person),
                      hintText: 'What do people call you?',
                      labelText: AppLocalizations.of(context).textField,
                    ),
                    onSaved: (String value) {
                      person.name = value;
                    },
                    validator: _validateName,
                  ),
                  const SizedBox(height: 24.0),
                  new TextFormField(
                    initialValue: person.page.toString(),
                    decoration: const InputDecoration(
                      contentPadding: EdgeInsets.all(0.0),
                      border: const UnderlineInputBorder(),
                      filled: true,
                      icon: const Icon(Icons.phone),
                      hintText: 'Where can we reach you?',
                      labelText: 'Phone Number *',
                      prefixText: '+1',
                    ),
                    keyboardType: TextInputType.phone,
                    onSaved: (String value) {
                      person.page = int.parse(value);
                    },
                    validator: _validatePhoneNumber,
                    // TextInputFormatters are applied in sequence.
                    inputFormatters: <TextInputFormatter>[
                      WhitelistingTextInputFormatter.digitsOnly,
                      // Fit the validating format.
                      _phoneNumberFormatter,
                    ],
                  ),
                  const SizedBox(height: 24.0),
                  new TextFormField(
                    initialValue: person.url,
                    decoration: const InputDecoration(
                      border: const UnderlineInputBorder(),
                      filled: true,
                      icon: const Icon(Icons.email),
                      hintText: 'Your email address',
                      labelText: 'E-mail',
                    ),
                    keyboardType: TextInputType.emailAddress,
                    onSaved: (String value) {
                      person.url = value;
                    },
                  ),
                  const SizedBox(height: 24.0),
                  new CommonPickerSimple(
                      labelText: '111',
                      hintText: '222',
                      activity: _activity,
                      allActivities: _allActivities,
                      selectActivity: (String newValue) {
                        setState(() {
                          _activity = newValue;
                        });
                      }),
                  const SizedBox(height: 24.0),
                  new TextFormField(
                    decoration: const InputDecoration(
                      border: const OutlineInputBorder(),
                      hintText: 'Tell us about yourself',
                      helperText: 'Keep it short, this is just a demo.',
                      labelText: 'Life story',
                    ),
                    maxLines: 3,
                  ),
                  const SizedBox(height: 24.0),
                  new TextFormField(
                    keyboardType: TextInputType.number,
                    decoration: const InputDecoration(
                        border: const OutlineInputBorder(),
                        labelText: 'Salary',
                        prefixText: '\$',
                        suffixText: 'USD',
                        suffixStyle: const TextStyle(color: Colors.green)),
                    maxLines: 1,
                  ),
                  const SizedBox(height: 24.0),
                  new PasswordField(
                    fieldKey: _passwordFieldKey,
                    helperText: 'No more than 8 characters.',
                    labelText: 'Password *',
                    onFieldSubmitted: (String value) {
                      setState(() {
                        person.address.city = value;
                      });
                    },
                  ),
                  const SizedBox(height: 24.0),
                  new TextFormField(
                    enabled: person.address.city != null &&
                        person.address.city.isNotEmpty,
                    decoration: const InputDecoration(
                      border: const UnderlineInputBorder(),
                      filled: true,
                      labelText: 'Re-type password',
                    ),
                    maxLength: 8,
                    obscureText: true,
                    validator: _validatePassword,
                  ),
                  const SizedBox(height: 24.0),
                  new Text('* indicates required field',
                      style: Theme.of(context).textTheme.caption),
                  const SizedBox(height: 24.0),
                ],
              ),
            ),
          ),
        ),
        bottomNavigationBar: new RaisedButton(
          child: const Text('SUBMIT'),
          onPressed: _handleSubmitted,
        ),
      );
    } else {
      return new Scaffold();
    }
  }

  @override
  void onLoadListFail() {
    print("---onLoadTestDetailFail---");
    setState(() {});
  }

  @override
  void onLoadListSuc(BaseModel<UserInfo> model) {
    if (!mounted) return; //异步处理，防止报错
    if (model.code != HttpStatus.OK) {
      CommonSnakeBar.buildSnakeBar(context, model.errorMsg);
      return;
    }
    _userInfo = model.data;

    print("---onLoadTestDetailSuc---" + _userInfo.toString());
    setState(() {
      person = _userInfo;
    });
  }

  @override
  setPresenter(TestListPresenter presenter) {
    _testListPresenter = presenter;
  }
}

/// Format incoming numeric text to fit the format of (###) ###-#### ##...
class _UsNumberTextInputFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    final int newTextLength = newValue.text.length;
    int selectionIndex = newValue.selection.end;
    int usedSubstringIndex = 0;
    final StringBuffer newText = new StringBuffer();
    if (newTextLength >= 1) {
      newText.write('(');
      if (newValue.selection.end >= 1) selectionIndex++;
    }
    if (newTextLength >= 4) {
      newText.write(newValue.text.substring(0, usedSubstringIndex = 3) + ') ');
      if (newValue.selection.end >= 3) selectionIndex += 2;
    }
    if (newTextLength >= 7) {
      newText.write(newValue.text.substring(3, usedSubstringIndex = 6) + '-');
      if (newValue.selection.end >= 6) selectionIndex++;
    }
    if (newTextLength >= 11) {
      newText.write(newValue.text.substring(6, usedSubstringIndex = 10) + ' ');
      if (newValue.selection.end >= 10) selectionIndex++;
    }
    // Dump the rest.
    if (newTextLength >= usedSubstringIndex)
      newText.write(newValue.text.substring(usedSubstringIndex));
    return new TextEditingValue(
      text: newText.toString(),
      selection: new TextSelection.collapsed(offset: selectionIndex),
    );
  }
}
